package com.wfy.modules.sys.dto;

import com.wfy.common.persistence.BaseDto;
import com.wfy.common.util.StringUtils;
import com.wfy.modules.sys.entity.Office;
import com.wfy.modules.sys.entity.Role;
import com.wfy.modules.sys.entity.User;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: wangxindong
 * Date: 2018/1/25
 * Time: 15:33
 */
public class UserDto extends BaseDto {

    // ================================================================
    // Constants
    // ================================================================

    // ================================================================
    // Fields
    // ================================================================

    private String id;
    /**
     * 用户名称
     */
    private String name;
    private String password;
    private String againPassword;
    private String officeId;
    private String email;
    private String phone;
    private String photo;
    private String remarks;
    private Integer userType;
    /**
     * 用户登录名
     */
    @NotNull(message = "请输入登录名称")
    private String loginName;

    /**
     * 角色集合
     */
    private Set<String> roles;

    // ================================================================
    // Constructors
    // ================================================================

    // ================================================================
    // Methods from/for super Interfaces or SuperClass
    // ================================================================

    @Override
    public String toString() {
        return "UserDto{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", againPassword='" + againPassword + '\'' +
                ", officeId='" + officeId + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", photo='" + photo + '\'' +
                ", remarks='" + remarks + '\'' +
                ", userType=" + userType +
                ", loginName='" + loginName + '\'' +
                ", roles=" + roles +
                '}';
    }


    // ================================================================
    // Public or Protected Methods
    // ================================================================

    public User build() {
        User user = new User();
        BeanUtils.copyProperties(this, user);
        Set<String> roles = this.getRoles();

        if (null != roles) {
            List<Role> roleList = new ArrayList<>(roles.size());
            Iterator<String> it = roles.iterator();
            while (it.hasNext()) {
                String r = it.next();
                Role role = new Role(r);
                roleList.add(role);
            }
            user.setRoleList(roleList);
        }
        String officeId = this.getOfficeId();
        if (StringUtils.isNotEmpty(officeId)) {
            Office office = new Office(officeId);
            user.setOffice(office);
        }
        return user;
    }


    // ================================================================
    // Getter & Setter
    // ================================================================

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAgainPassword() {
        return againPassword;
    }

    public void setAgainPassword(String againPassword) {
        this.againPassword = againPassword;
    }

    public String getOfficeId() {
        return officeId;
    }

    public void setOfficeId(String officeId) {
        this.officeId = officeId;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public Integer getUserType() {
        return userType;
    }

    public void setUserType(Integer userType) {
        this.userType = userType;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public Set<String> getRoles() {
        return roles;
    }

    public void setRoles(Set<String> roles) {
        this.roles = roles;
    }

    // ================================================================
    // Private Methods
    // ================================================================


    // ================================================================
    // Inner or Anonymous Class
    // ================================================================

    // ================================================================
    // Test Methods
    // ================================================================

}
